package it.sd.wdc.spider.repository.template;


import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TemplateParserHandler extends DefaultHandler{

	private SpiderNodeTemplate root = null;
//	private StringBuffer buffer = null;
	
	private Stack<SpiderNodeTemplate> stack = new Stack<SpiderNodeTemplate>();
	
	public SpiderNodeTemplate getNodeTemplate() {
		return this.root;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
//		buffer.append(new String(ch, start, length));
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		stack.pop();
		
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		
		SpiderNodeTemplate current = new SpiderNodeTemplate(qName);
	
		current.setRegexp(atts.getValue("regexp"));
		current.setItem(atts.getValue("item"));
		
		try {
			current.setIgnoreContent("true".equalsIgnoreCase(atts.getValue("ignoreContent")));
			current.setRemoveTag("true".equalsIgnoreCase(atts.getValue("removeTag")));
			current.setTrimSpace("true".equalsIgnoreCase(atts.getValue("trimSpace")));
		} catch(Exception e) {
			
		}
		
		if (root == null) {
			root = current;
		}
		
		if (stack.size()>0) {
			stack.peek().addFiglio(current);
		}
		stack.push(current);
		
	}

	
}
